package com.ybkj.daijia.api.apirest.v4;

import com.ybkj.daijia.ServiceSetting;
import com.ybkj.daijia.api.driver.transport.ResultTransport;
import com.ybkj.daijia.api.util.ErrorConstantV4;
import com.ybkj.daijia.server.mc.Passenger;
import com.ybkj.daijia.server.mc.PassengerGrade;
import com.ybkj.daijia.server.service.ErrandService;
import com.ybkj.daijia.server.service.FreightService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.OrderService;
import com.ybkj.daijia.server.service.ZhuancheService;
import com.ybkj.daijia.server.service.ZhuanxianService;
import com.ybkj.daijia.serverUtils.ServiceUtil;
import java.util.HashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;


@Controller("ApiPassengerProcessControllerV4")
@RequestMapping(value = "api/passenger/v4/")
public class PassengerProcessController {

    public static final String ORDER_TYPE_ZHUANXIAN = "cjzx";
    public static final String ORDER_TYPE_ZHUANCHE = "zc";
    @Autowired
    private OrderService orderService;
    @Autowired
    private ErrandService errandService;
    @Autowired
    private ZhuancheService zhuancheService;
    @Autowired
    private FreightService freightService;
    @Autowired
    private ZhuanxianService zhuanxianService;
    @Autowired
    private MemberService memberService;

    /**
     * @param passengerId
     * @return
     * @record zhyu 查询各个系统由微信,后代,乘客端创建的订单中已经到达目的但还没有支付的订单的数量
     */
    @RequestMapping(value = "countNotPayOrders")
    public @ResponseBody
    ResultTransport countNotPayOrders(Long passengerId) {

        if (null == passengerId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        try {

            //如果存在未支付订单，不能下单
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            Long notPayAmounts = 0L;
//            notPayAmounts += orderService.countNotPayByPassenger(passengerId);
//            if (serviceSetting.isPaotui()) {
//                notPayAmounts += errandService.countNotPayByPassenger(passengerId);
//            }
            if (serviceSetting.isZhuanche()) {
                notPayAmounts += zhuancheService.countNotPayByPassenger(passengerId);
            }
//            if (serviceSetting.isFreight()) {
//                notPayAmounts += freightService.countNotPayByPassenger(passengerId);
//            }
            if (serviceSetting.isZhuanxian()) {
                notPayAmounts += zhuanxianService.countNotPayByPassenger(passengerId);
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(notPayAmounts);

            return instance;

        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "getWhichNotPayOrder")
    public @ResponseBody
    ResultTransport getWhichNotPayOrder(Long passengerId) {

        if (null == passengerId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        try {

            //如果存在未支付订单，不能下单
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            String whichNotPay = null;

            if (serviceSetting.isDaijia()) {
                Long notPayAmounts = orderService.countNotPayByPassenger(passengerId);
                if (notPayAmounts > 0) {
                    whichNotPay = "daijia";
                }
            }
            if (serviceSetting.isZhuanche() && null == whichNotPay) {
                Long notPayAmounts = zhuancheService.countNotPayByPassenger(passengerId);
                if (notPayAmounts > 0) {
                    whichNotPay = "zhuanche";
                }
            }
            if (serviceSetting.isPaotui() && null == whichNotPay) {
                Long notPayAmounts = errandService.countNotPayByPassenger(passengerId);
                if (notPayAmounts > 0) {
                    whichNotPay = "errand";
                }
            }
            if (serviceSetting.isFreight() && null == whichNotPay) {
                Long notPayAmounts = freightService.countNotPayByPassenger(passengerId);
                if (notPayAmounts > 0) {
                    whichNotPay = "freight";
                }
            }
            if (serviceSetting.isZhuanxian() && null == whichNotPay) {
                Long notPayAmounts = zhuanxianService.countNotPayByPassenger(passengerId);
                if (notPayAmounts > 0) {
                    whichNotPay = "zhuanxian";
                }
            }

            if (null == whichNotPay) {
                whichNotPay = "none";
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(whichNotPay);

            return instance;

        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "getWhichNotFinishOrder")
    public @ResponseBody
    ResultTransport getWhichNotFinishOrder(Long passengerId, String type) {

        if (null == passengerId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }

        try {

            //如果存在未支付订单，不能下单
            ServiceSetting serviceSetting = ServiceUtil.checkOpen();
            String whichNotPay = null;

            Long daijia = 0L;
            Long zhuanche = 0L;

            if (serviceSetting.isDaijia()) {
                if (StringUtils.isBlank(type) || "daijia".equals(type)) {
                    daijia = orderService.countNotFinishedByPassenger(passengerId);
                    if (daijia > 0) {
                        whichNotPay = "daijia";
                    }
                }
            }

            if (null == whichNotPay) {
                if (serviceSetting.isZhuanche()) {
                    if (StringUtils.isBlank(type) || "zhuanche".equals(type)) {
                        zhuanche = zhuancheService.countNotFinishedByPassenger(passengerId);
                        if (zhuanche > 0) {
                            whichNotPay = "zhuanche";
                        }
                    }
                }
            }

            if (null == whichNotPay) {
                whichNotPay = "none";
            }

            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            instance.setData(whichNotPay);

            return instance;

        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }

    @RequestMapping(value = "getParallelOrderNumber")
    public @ResponseBody
    ResultTransport getParallelOrderNumber(Long passengerId,
        @RequestParam(required = false) String type) {
        if (null == passengerId) {
            return ResultTransport.getErrorBy(ErrorConstantV4.PARAM_ERROR);
        }
        try {
            Long otherCount = null;
            ResultTransport instance = ResultTransport.getErrorBy(ErrorConstantV4.SUCCESS);
            if (type != null) {
                Map<String, Object> ex = new HashMap<>(2);
                if (type.equals(ORDER_TYPE_ZHUANXIAN)) {
                    otherCount = zhuancheService.countActiveByPassenger(passengerId);
                    if (otherCount != null) {
                        ex.put(ORDER_TYPE_ZHUANXIAN, otherCount);
                        Long parallelOrderNumber = null;
                        Passenger passenger = memberService.findOnePassenger(passengerId);
                        if (null != passenger.getGrade_id()) {
                            PassengerGrade pg = memberService.findOneGrade(passenger.getGrade_id());
                            if (null != pg) {
                                parallelOrderNumber = pg.getParallelOrderNumber();
                            }
                        }
                        instance.setData(parallelOrderNumber);
                    }
                } else if (type.equals(ORDER_TYPE_ZHUANCHE)) {
                    otherCount = zhuanxianService.countRunOrderByPassenger(passengerId);
                    if (otherCount != null) {
                        ex.put(ORDER_TYPE_ZHUANCHE, otherCount);
                    }
                }
                instance.setSettings(ex);
            }
            return instance;
        } catch (Exception e) {
            return ResultTransport.getErrorBy(ErrorConstantV4.SERVER_ERROR);
        }
    }
}
